package edu.spec.pms.controller;

import edu.spec.pms.model.dto.MvcReturn;
import org.madgecko.basic.model.Pager;
import edu.spec.pms.model.dto.ExtGridReturn;
import edu.spec.pms.model.student.Clazz;
import edu.spec.pms.service.iface.IClazzService;
import edu.spec.pms.service.iface.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/clazz")
public class ClazzController {

	@Autowired
	private IClazzService clazzService;
	@Autowired
	private IUserService userService;

	@ResponseBody
	@RequestMapping("/add")
	public MvcReturn add(Clazz clazz, HttpSession session){
		clazz.setCreateDate(new Date());
//		User user = (User)session.getAttribute("user");
		clazz.setStudentNumber(0);
		clazzService.add(clazz);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/delete/{id}")
	public MvcReturn delete(@PathVariable Integer id){
		clazzService.delete(id);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/update")
	public MvcReturn update(Clazz clazz){
		Clazz dbc = clazzService.load(clazz.getId());
		dbc.setDes(clazz.getDes());
		dbc.setName(clazz.getName());
		dbc.setUpdateDate(new Date());
		clazzService.update(dbc);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/toggleStatus/{id}")
	public MvcReturn toggleStatus(@PathVariable Integer id){
		Clazz dbc = clazzService.load(id);
		if(dbc.getStatus()==0){
			dbc.setStatus(1);
		}else if(dbc.getStatus()==1){
			dbc.setStatus(0);
		}
		clazzService.update(dbc);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/findClazzByCon")
	public ExtGridReturn findClazzByCon(Integer status, String con){
		Pager<Clazz> pc = clazzService.findClazzByCon(status, con);
		return new ExtGridReturn(pc.getTotal(), pc.getDatas());
	}

	@ResponseBody
	@RequestMapping("/listInSchoolClazz")
	public ExtGridReturn listInSchoolClazz(){
		List<Clazz> lc = clazzService.listInSchoolClazz();
		return new ExtGridReturn((long)lc.size(), lc);
	}

}
